package com.huanshi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huanshi.entity.OmsOrder;
import com.huanshi.entity.OmsOrderItem;
import com.huanshi.mapper.OmsOrderItemMapper;
import com.huanshi.mapper.OmsOrderMapper;
import com.huanshi.module.oms.param.OrderParam;
import com.huanshi.module.oms.vo.OrderVo;
import com.huanshi.service.OmsOrderService;
import com.huanshi.utils.BeanCopyUtil;
import com.huanshi.utils.OrderNoCreate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 幻世
 * @since 2023-10-12
 */
@Service
public class OmsOrderServiceImpl extends ServiceImpl<OmsOrderMapper, OmsOrder> implements OmsOrderService {

    @Resource
    OmsOrderMapper orderMapper;
    @Resource
    OmsOrderItemMapper orderItemMapper;

    @Override
    public Long createOrder(OrderParam orderParam) {
        try {
            OmsOrder order = new OmsOrder();
            List<OmsOrderItem> orderItemList = orderParam.getOrderItemList();
            BeanCopyUtil.copyProperties(orderParam, order);
            String orderSn = OrderNoCreate.randomOrderCode();
            order.setOrderSn(orderSn);
            order.setCreateTime(LocalDateTime.now());
            order.setModifyTime(LocalDateTime.now());
            orderMapper.insert(order);
            OmsOrder orderStatus = orderMapper.selectOne(new LambdaQueryWrapper<OmsOrder>().eq(OmsOrder::getOrderSn, orderSn).select(OmsOrder::getId));
            orderItemList.forEach(orderItem -> {
                orderItem.setId(null);
                orderItem.setOrderId(orderStatus.getId());
                orderItem.setOrderSn(orderSn);
                orderItemMapper.insert(orderItem);
            });
            return orderStatus.getId();
        } catch (Exception e) {
            return (long) -1;
        }
    }

    @Override
    public Page<OrderVo> findPage(Page<OmsOrder> page, Integer status) {
        LambdaQueryWrapper<OmsOrder> queryWrapper = new LambdaQueryWrapper<>();
        if (status != null) {
            queryWrapper.eq(OmsOrder::getStatus, status);
        }
        Page<OmsOrder> orderPage = orderMapper.selectPage(page, queryWrapper);
        Page<OrderVo> orderVoPage = BeanCopyUtil.copyProperties(orderPage, OrderVo.class);
        orderVoPage.getRecords().forEach(item -> {
            item.setOrderItemList(orderItemMapper.selectList(new LambdaQueryWrapper<OmsOrderItem>()
                                                                    .eq(OmsOrderItem::getOrderId, item.getId())));

        });
        return orderVoPage;
    }

    @Override
    public OrderVo getByOrderSn(String orderSn) {
        OrderVo orderVo = new OrderVo();
        OmsOrder order = orderMapper.selectOne(new LambdaQueryWrapper<OmsOrder>()
                .eq(OmsOrder::getOrderSn, orderSn));
        List<OmsOrderItem> orderItemList = orderItemMapper.selectList(new LambdaQueryWrapper<OmsOrderItem>()
                .eq(OmsOrderItem::getOrderSn, orderSn));
        BeanCopyUtil.copyProperties(order, orderVo);
        orderVo.setOrderItemList(orderItemList);
        return orderVo;
    }

}
